package cn.gzsxt.music.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gzsxt.music.query.PowerQueryObject;
import cn.gzsxt.music.query.RoleQueryObject;
import cn.gzsxt.music.service.PowerService;
import cn.gzsxt.music.service.RoleService;
import cn.gzsxt.music.vo.MsgObj;

@Controller
@Scope("request")
@RequestMapping("/role")
public class RoleController {
	
	@Autowired
	private RoleService service;
	
	@Autowired
	private PowerService powerService;
	
	@RequestMapping("rolePage")
	public String rolePage() {
		return "rolePage";
	}
	
	
	@RequestMapping("roleList")
	@ResponseBody
	public Map<String, Object> roleList(RoleQueryObject qo) {
		System.out.println(qo);
		Map<String, Object> result = service.selectRoleDataByCondition(qo);
		return result;
	}
	
	
	//查询出所有的权限以供新增或者修改角色时候分配权限使用
	@RequestMapping("getAllPower")
	@ResponseBody
	public Map<String, Object> getAllPower(PowerQueryObject qo) {
		System.out.println(qo);
		 Map<String, Object> result = powerService.selectPowerDataByCondition(qo);
		return result;
	}
	
	@RequestMapping("addRole")
	@ResponseBody
	public MsgObj addRole(@RequestParam Map<String, Object> params) {
		
		int row = service.saveRole(params);
		
		MsgObj msgObj = new MsgObj(0, "添加角色失败");
		if(row == 1) {
			msgObj = new MsgObj(1, "添加角色成功");
		}
		return msgObj;
	}
	
	@RequestMapping("updateRole")
	@ResponseBody
	public MsgObj updateRole(@RequestParam Map<String, Object> params) {
		
		int row = service.updateRow(params);
		
		MsgObj msgObj = new MsgObj(0, "修改角色失败");
		if(row == 1) {
			msgObj = new MsgObj(1, "修改角色成功");
		}
		
		return msgObj;
	}
	
	
	@RequestMapping("deleteRoleById")
	@ResponseBody
	public MsgObj deleteRoleById(Long role_id) {
		
		int row  = service.deleteRoleById(role_id);
		
		MsgObj msgObj = new MsgObj(0, "删除角色失败");
		if(row == 1) {
			msgObj= new MsgObj(1, "删除角色成功");
		}
		return msgObj;
	}
	//用户修改权限时候,会显我的拥有的权限相关数据
	@RequestMapping("myPower")
	@ResponseBody
	public Map<String, Object> myPower(String role_action) {
		//role_action : admin/addAdmin.do,admin/deleteAdminById,admin/updateAdmin.do,admin/adminList.do
		Map<String, Object> result = powerService.getPowerByPowerActions(role_action);
		
		return result;
	}
	
	//获取系统所有角色,不带分页的,以供用户添加角色使用
	@RequestMapping("getAllRoles")
	@ResponseBody
	public List<Map<String, Object>> getAllRoles() {
		List<Map<String, Object>> result = service.getAllRoles();
		
		return result;
	}
	
	
	
	
}
